package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.PlatformPayData;
import com.wgts.wgts.control.eventbus.PutForwardEventBus;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerPutForwardActivityComponent;
import com.wgts.wgts.dagger.module.PutForwardActivityModule;
import com.wgts.wgts.mvp.presenter.IPutForwardActivityPresenter;
import com.wgts.wgts.mvp.view.IPutForwardActivityView;
import com.wgts.wgts.mvp.view.activity.setPassWord.SettingPasswordActivity;
import com.wgts.wgts.netmodle.resp.MyCenterMessageEntity;
import com.wgts.wgts.netmodle.resp.PutForwardTypeEntity;
import com.wgts.wgts.tools.AppInfor;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.umeng.socialize.utils.ContextUtil.getContext;

public class PutForwardActivity extends BaseActivity implements IPutForwardActivityView {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_put_forward_order)
    TextView tvPutForwardOrder;
    @BindView(R.id.tv_mobi)
    TextView tvMobi;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_util)
    TextView tvUtil;
    @BindView(R.id.edit_mobi)
    EditText editMobi;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.tv_detail_content)
    TextView tvDetailContent;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.tv_over)
    TextView tvOver;
    @BindView(R.id.img_more)
    ImageView imgMore;
    @BindView(R.id.relative_title)
    RelativeLayout relativeTitle;
    @BindView(R.id.linear_add)
    LinearLayout linearAdd;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.placeholder_ly)
    RelativeLayout placeholderLy;
    @BindView(R.id.linear_ensure)
    LinearLayout linearEnsure;
    private PutForwardFragmentDialog dialog;

    private PutForwardAdapter adapter;

    @Inject
    IPutForwardActivityPresenter presenter;

    private PutForwardTypeEntity selectEntity;

    private double mobi;

    private static final int ADDPUTFORWARDACTIVITY_QUEST = 0x0020;

    private List<PutForwardTypeEntity> putForwardTypeEntities;

    @Override
    public void init() {
        DaggerPutForwardActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .putForwardActivityModule(new PutForwardActivityModule(this))
                .build()
                .inject(this);
        mobi = Double.valueOf(AppInfor.getInstance().getAvailableBalance());
        putForwardTypeEntities = new ArrayList<>();
    }

    @Override
    public void onCreate(Bundle saveInstancBundle) {
        super.onCreate(saveInstancBundle);
        setContentView(R.layout.put_forward_activity);
        ButterKnife.bind(this);
        initView();
    }

    @OnClick({R.id.linear_ensure, R.id.img_back, R.id.tv_over, R.id.linear_add, R.id.img_more, R.id.tv_put_forward_order})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_ensure:
                ensure();
                break;
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_over:
                adapter.setEdit(false);
                adapter.notifyDataSetChanged();
                //imgMore.setVisibility(View.VISIBLE);
                //tvOver.setVisibility(View.GONE);
                break;
            case R.id.linear_add:
                clickAdd();
                break;
            case R.id.img_more:
//                adapter.setEdit(true);
//                adapter.notifyDataSetChanged();
//                imgMore.setVisibility(View.GONE);
//                tvOver.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_put_forward_order:
                Intent intent = new Intent(PutForwardActivity.this, ForwardOrderActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void forwardOrder() {
        //Intent intent = new Intent(this, )
    }

    private void clickAdd() {
        Intent intent = new Intent(this, AddPutForwardActivity.class);
        startActivityForResult(intent, ADDPUTFORWARDACTIVITY_QUEST);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        presenter.initPutForward();
    }

    private void jumpActivity(Class activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }

    private void initView() {

        adapter = new PutForwardAdapter(this, putForwardTypeEntities);
        recycleView.setAdapter(adapter);

        adapter.setOnClickListenter(new PutForwardAdapter.OnClickListenter() {
            @Override
            public void ondel(int i, View view) {
                //presenter.delPutForward(i);
            }

            @Override
            public void onSelect(int i, View view) {
                presenter.selectPutForward(i);
            }
        });

        presenter.initPutForward();

        tvMobi.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        editMobi.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        CustomLineaLayoutManager linearLayoutManager = new CustomLineaLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);

        tvMobi.setText(String.valueOf(mobi));

        tvTip.setText("本次最大可提现" + String.valueOf(mobi) + "元");

        editMobi.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //删除.后面超过两位的数字
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        editMobi.setText(s);
                        editMobi.setSelection(s.length());
                    }
                }

                //如果.在起始位置,则起始位置自动补0
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editMobi.setText(s);
                    editMobi.setSelection(2);
                }

                //如果起始位置为0并且第二位跟的不是".",则无法后续输入
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editMobi.setText(s.subSequence(0, 1));
                        editMobi.setSelection(1);
                        return;
                    }
                }

                if (!s.toString().equals("")) {
                    if (Double.valueOf(s.toString()) <= mobi) {
                        tvTip.setText("本次最大可提现" + String.valueOf(mobi) + "元");
                        tvTip.setTextColor(ContextCompat.getColor(PutForwardActivity.this, R.color.hint_6));

                    } else {
                        tvTip.setText("余额不足");
                        tvTip.setTextColor(ContextCompat.getColor(PutForwardActivity.this, R.color.delete_red));
                    }
                } else {
                    tvTip.setText("本次最大可提现" + String.valueOf(mobi) + "元");
                    tvTip.setTextColor(ContextCompat.getColor(PutForwardActivity.this, R.color.hint_6));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });

    }


    private void ensure() {

        if (editMobi.getText().toString().equals("")) {
            MoHuToast.showToast("请输入提现金额");
            return;
        }

        if (Double.valueOf(editMobi.getText().toString()) > mobi) {
            MoHuToast.showToast("余额不足");
            return;
        }

        if (selectEntity == null) {
            MoHuToast.showToast("请选择提现方式");
            return;
        }

        if (editMobi.getText().toString().equals("0") || editMobi.getText().toString().equals("0.0") || editMobi.getText().toString().equals("0.00")) {
            MoHuToast.showToast("提现金额须大于0");
            return;
        }

        showDialog();
        presenter.userInfo();

    }

    @Override
    public void initPutForwardList(List<PutForwardTypeEntity> entities) {
//        if(entities.size() == 0){
//            imgMore.setVisibility(View.INVISIBLE);
//        }else{
//            imgMore.setVisibility(View.VISIBLE);
//        }

        if (entities.size() > 0) {
            entities.get(0).setChoese(true);
            selectEntity = entities.get(0);
        }

        putForwardTypeEntities.clear();
        putForwardTypeEntities.addAll(entities);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void putForwardSuccess() {
        Intent intent = new Intent(this, PutForwardProgressActivity.class);
        startActivity(intent);
        MoHuToast.showToast("提现成功");
        EventBus.getDefault().post(new PutForwardEventBus());
        finish();
    }

    @Override
    public void delPutForward(int i) {
        if (putForwardTypeEntities.size() > i) {
            putForwardTypeEntities.remove(i);
        }

//        if(putForwardTypeEntities.size() == 0){
//            imgMore.setVisibility(View.INVISIBLE);
//        }else{
//            imgMore.setVisibility(View.VISIBLE);
//        }


        adapter.notifyDataSetChanged();
    }

    @Override
    public void selectPutForwardType(PutForwardTypeEntity entity) {
        selectEntity = entity;
        adapter.notifyDataSetChanged();
    }

    @Override
    public void userInfo(MyCenterMessageEntity entity) {
        dialogDismiss();
        if (entity.modifyPayPassword) {
            PlatformPayData data = new PlatformPayData();
            data.setPayPrice(editMobi.getText().toString());
            data.setOrderInfo("");
            data.setResidueCoin("");
            dialog = PutForwardFragmentDialog.getPlatformPayDialogFragment(data);
            dialog.setInputOverListener(new PutForwardFragmentDialog.InputOverListener() {
                @Override
                public void inputOver(String s) {
                    presenter.putForward(editMobi.getText().toString(), selectEntity.cardId, s);
                }
            });

            dialog.show(getSupportFragmentManager(), "");
        } else {

            Intent intent = new Intent(PutForwardActivity.this, SettingPasswordActivity.class);
            startActivity(intent);

        }
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }
}
